<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>药品操作表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/res/layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
    <script type="text/javascript" src="/res/js/jquery-3.4.1.js"></script>
    <script src="/res/js/util.js" charset="utf-8"></script>

</head>
<body>

<form class="layui-form" action="" lay-filter="myform" id="myform" enctype="multipart/form-data">
    <input type="hidden" name="drugId" >
    <div class="layui-form-item">
        <label class="layui-form-label">药品名称</label>
        <div class="layui-input-block">
            <input type="text" name="drugName" lay-verify="title" autocomplete="off" placeholder="请输入药品名称" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">药品类型</label>
        <div class="layui-input-block">
            <select name="typeId" id="typeId" >

            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">零售价</label>
        <div class="layui-input-block">
            <input type="text" name="drugRetail" maxlength='8' lay-verify="nums" autocomplete="off" placeholder="请输入零售价" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">批发价</label>
        <div class="layui-input-block">
            <input type="text" name="drugWho" maxlength='8' lay-verify="nums" autocomplete="off" placeholder="请输入批发价" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">出库价</label>
        <div class="layui-input-block">
            <input type="text" name="drugOut" maxlength='8' lay-verify="nums" autocomplete="off" placeholder="请输入出库价" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">药品图片</label>
        <div class="layui-input-block">
            <input type="file" name="file" id="images" onchange="show(this)">
            <img src="" id="prePic" alt="图片" width="50" height="50">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
        </div>
    </div>
</form>

<script src="/res/layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use(['form', 'layedit', 'laydate'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate


        //自定义验证规则
        form.verify({
            title: function(value){
                if(value.length < 1){
                    return '标题至少得1个字符啊';
                }
                if (value.length > 50) {
                    return '填入过长';
                }
            }
            ,nums: [
                /^\d+$|^\d+[.]?\d+$/
                ,'必须填写数字'
            ]
            ,content: function(value){
                layedit.sync(editIndex);
            }
        });

        // var loading = layer.load(0, {
        //     shade: true,
        //     time: 2*1000
        // });
        //监听提交
        form.on('submit(demo1)', function(data){
            /* layer.alert(JSON.stringify(data.field), {
              title: '最终的提交信息'
            }) */
            var loa ;
            var url = "/hqDrug/addOrUpdate";
            var formData = new FormData(document.getElementById("myform"));
            $.ajax({
                url : url,
                data : formData,
                processData : false,
                contentType : false,
                dataType : 'json',
                type : 'POST',
                beforeSend:function(){
                  loa = layer.load(0,{shade:true,time: 8*1000});
                },
                success : function(bm){
                    layer.msg(bm.message)
                    if(bm.status==1){
                        layer.close(loa);
                        parent.closeOpen(bm.message);
                    }
                },
                error : function(bm){
                    parent.closeOpen(bm.message);
                }

            });

            return false;
        });

        var url = "/hqDrug/selectType";
        var data = null;
        $.post(url,data,function(obj){
            $.each(obj,function(index,item){
                $("#typeId").append("<option value="+item.typeId+">"+item.typeName+"</option>");
            });
            form.render("select");
        },"json")

//表单赋值
        var drugId = getQueryString("id");
        if(drugId){
            var url = "/hqDrug/selectOne";
            var data = {id:drugId};
            $.post(url,data,function(obj){
                console.info(obj);
                form.val('myform',obj);
                console.info(obj.images);
                $("#prePic").prop("src",obj.images);
            },"json")}

    });

    function show(file) {
        var reader = new FileReader();	// 实例化一个FileReader对象，用于读取文件
        var img = document.getElementById('prePic'); 	// 获取要显示图片的标签

        //读取File对象的数据
        reader.onload = function (evt) {
            img.width = "80";
            img.height = "80";
            img.src = evt.target.result;
        }
        reader.readAsDataURL(file.files[0]);
    }
</script>

</body>
</html>